<?php
/**
 * Description: 获取公开课列表
 * User: devforma
 * Date: 15/12/31
 * Time: 15:39
 */

require LIB_DIR . '/LiveCourse.class.php';

class GetOpenClass implements iController {
    private $url = 'http://spark.appublisher.com/plain/static_open_course?class_id=';

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        $openCourses = $this->getOpenCourses();

        $playbacks = $this->getPlaybacks();

        return genOutput(1, array(
            'courses' => $openCourses,
            'playbacks' => $playbacks
        ));
    }

    /**
     * 获取回放列表
     *
     * @return array
     */
    private function getPlaybacks() {
        global $cmsdb;

        $ids = getOption('staticCourseUrl');

        $sql = "SELECT cl.id AS class_id, co.id AS course_id, co.course_name, co.rate_score, co.rate_num, cl.room_id, co.introduction, cl.courseware_link, co.cover_pic
        FROM `data_live_class` AS cl, `relationship_live_class_and_course` AS r, `data_live_course` AS co
        WHERE cl.id=r.class_id AND r.course_id=co.id AND co.type_id=1 AND cl.id IN ({$ids}) ORDER BY FIELD (cl.id, {$ids})";
        $courses = $cmsdb->getResults($sql);

        $list = array();
        foreach ($courses as $course) {
            $classInfo = getLiveClassInfo($course->class_id);

            $startTime = strtotime($classInfo->start_time);
            $endTime = strtotime($classInfo->end_time);

            $list[] = array(
                'course_id' => intval($course->course_id),
                'class_id' => intval($course->class_id),
                'name' => $course->course_name,
                'lector' => $classInfo->lector_name,
                'score' => round($course->rate_score, 1),
                'persons_num' => $this->getPersonsNum($course->class_id, $course->room_id),
                'rate_num' => intval($course->rate_num),
                'url' => $this->url . $course->class_id,
                'introduction' => $course->introduction,
                'material' => $course->courseware_link,
                'cover_pic' => getSecureUrl($course->cover_pic),
                'start_time' => $classInfo->start_time,
                'date' => date('m月d日 H:i', $startTime) . ' - ' .date('H:i', $endTime),
            );
        }

        return $list;
    }


    /**
     * 获取公开课出席人数
     *
     * @param $classId
     * @param $roomId
     * @return int
     */
    private function getPersonsNum($classId, $roomId) {
        if (empty($classId))
            return 0;

        $cacheKey = 'func:getPersonsNum:'.$classId;

        if (($num = cacheGet($cacheKey)) == null) {
            $num = LiveCourse::getClassPersons($roomId);
            cacheAdd($cacheKey, $num, 10*60);
        }

        return $num;
    }


    /**
     * 获取正在直播或即将开始的公开课
     *
     * @return array
     */
    private function getOpenCourses() {
        global $cmsdb;

        $now = time();
        $sql = "SELECT cl.id AS class_id, co.id AS course_id, co.course_name, co.introduction, cl.courseware_link, co.cover_pic
        FROM `data_live_class` AS cl, `relationship_live_class_and_course` AS r, `data_live_course` AS co
        WHERE cl.id=r.class_id AND r.course_id=co.id AND co.type_id=1 AND co.publish_status='publish'
        AND UNIX_TIMESTAMP(cl.end_time)>{$now} ORDER BY cl.start_time ASC";

        $results = $cmsdb->getResults($sql);

        $list = array();
        foreach ($results as $result) {
            $classInfo = getLiveClassInfo($result->class_id);

            $startTime = strtotime($classInfo->start_time);
            $endTime = strtotime($classInfo->end_time);

            $list[] = array(
                'id' => intval($result->course_id),
                'name' => $result->course_name,
                'date' => date('m月d日 H:i', $startTime) . ' - ' .date('H:i', $endTime),
                'lector' => $classInfo->lector_name,
                'introduction' => $result->introduction,
                'material' => $result->courseware_link,
                'cover_pic' => getSecureUrl($result->cover_pic),
                'start_time' => $classInfo->start_time,
                'is_onair' => $endTime > $now && $startTime - 10*60 <= $now
            );
        }

        return $list;
    }
}